<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/17
 * Time: 8:42
 */

namespace app\admin\model;


use app\lib\SendTemplate;
use think\Model;
use think\Db;

class OrderModel extends Model
{
    // 确定链接表名
    protected $name = 'orders';

    /**
     * 查询订单
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getOrderByWhere($where,$offset=0, $limit=10)
    {

        return  $this->where($where)->group('order_mun')->order('id desc')->limit($offset, $limit)->select();
    }
    //下单时间
    public function getAddtimeAttr($v){
        return date('Y-m-d H:i:s',$v);
    }
    //服务时间
    public function getServcietimeAttr($v){
        return date('Y-m-d H:i',$v);
    }

    public function getStatusAttr($v){
       $msg = config('order_status');
       return $msg[$v];
    }
    public function getTypeAttr($v){
        $msg = ['','商城订单','后台发布'];
        return $msg[$v];
    }

    public function getShuxingidAttr($v){
//        if (!empty($v)){
//            return ShifuModel::get($v)['name'];
//        }
    }



    /**
     * 根据搜索条件获取所有的订单数量
     * @param $where
     */
    public function getAllOrder($where)
    {
        return    $this->where($where)->group('order_mun')->order('id desc')->count();
    }


    public function add($param){
        try{
            $param['addtime']      = time();
            $param['a_time']       = time();
            $param['status']       = 2;
            $param['type']         = 2;
            $param['munber']       = 1;
            $param['servcietime']  = strtotime($param['servcietime']);
            $param['order_mun']    = date('YmdHis',time()).rand(1000,10000);
            $param['addr']         = AreaModel::get($param['province'])['name'].'/'.AreaModel::get($param['city'])['name'].'/'.AreaModel::get($param['county'])['name'].'/'.$param['addr'];
            $param['price']        = 0;//isset($param['shuxingid'])&& !empty($param['shuxingid'])? Shuxing::get($param['shuxingid'])['price']:GoodsModel::get($param['goodsid'])['shop_price'];
            $param['goods_name']   = GoodsModel::get($param['goodsid'])['goods_name'];
            isset($param['shuxingid'])&& !empty($param['shuxingid'])?$param['shuxing_name'] = Shuxing::get($param['shuxingid'])['name']:'';
            unset($param['shuxingid']);
            unset($param['goodsid']);
            if ($this->save($param)){
                return msg(1,url('index'),'保存成功!');
            }else{
                throw new \Exception($this->getError());
            }
        }catch (\Exception $e){
            return msg(-1,[],$e->getMessage());
        }
    }



    /**
     * 推送模板信息
     * @param $param
     */
    public function editOrder($param)
    {
        Db::startTrans();
        try{
            $user = Member::where('id','in',implode(',',$param['user_id']))->select();
            if (!$user)throw new \Exception('参数错误!');
            $openIdsArr = array_column((array)$user,'wxopenid');
//            $f = fopen('openid.txt','a+');
//            fwrite($f,json_encode($openIdsArr));
//            fclose($f);
//            $IdsArr = array_column((array)$user,'id');
//            $newArr= [];
//            foreach ($IdsArr as $k => $v){
//                $newArr[$k]['user_id']      = $v;
//                $newArr[$k]['addtime']      = time();
//                $newArr[$k]['order_nunber'] = $param['id'];
//            }
            (new SendTemplate($param['id'],$openIdsArr))->toDoSend(1);
//            if((new SendSms())->saveAll($newArr)){
//                (new SendTemplate($param['id'],$openIdsArr))->toDoSend(1);
//            }
            return msg(1,url('index'),'发布成功');
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * 根据订单的id 获取订单的信息
     * @param $id
     */
    public function getOneOrderle($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 删除订单
     * @param $id
     */
    public function delOrderle($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除订单成功');

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }

}